package com.sc.test.recyclerview2;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.sc.test.R;
import com.sc.test.RecyclerBaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Shen Chao on 2018-03-13-013 上午 8:36.
 */

public class StaggeredRvActivity extends RecyclerBaseActivity {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private StaggeredRvAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv_staggered);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initData() {
        mLayoutManager = new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL);
        mAdapter = new StaggeredRvAdapter(InitData.getData());
        mAdapter.setOnItemClickListener(new StaggeredRvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(StaggeredRvActivity.this,
                        "click " + position + " item", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(StaggeredRvActivity.this,
                        "long click " + position + " item", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.menu_add_item:
                mAdapter.addNewItem();
                mLayoutManager.scrollToPosition(0);
                return true;
            case R.id.menu_delete_item:
                mAdapter.deleteItem();
                mLayoutManager.scrollToPosition(0);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
